﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECBS_Backend.dao;
using ECBS_Backend.model;
using Microsoft.AspNetCore.Mvc;
using ECBS_Backend.service;
using ECBS_Backend.utils;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ECBS_Backend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {

        private readonly StudentService studentService;
        private readonly EnrollmentService enrollmentService;
        private readonly GradeService gradeService;

        public StudentController(StudentService studentService, EnrollmentService enrollmentService, GradeService gradeService)
        {
            this.studentService = studentService;
            this.enrollmentService = enrollmentService;
            this.gradeService = gradeService;
        }

        // POST api/values
        /// <summary>
        /// 学生登录
        /// </summary>
        /// <param name="userName">学号</param>
        /// <param name="password">密码</param>
        [HttpGet("login")]
        public ActionResult<string> Login([FromQuery]string userName, [FromQuery]string password, [FromQuery]string schoolName)
        {
            try{
                Student s =  studentService.StudentLogin(userName, password, schoolName);
                string token = JwtGenerator.GenerateJWT("Student", userName);

                WeIDToken weIDToken = new WeIDToken { WeID = s.WeID, Token = token };

                return Ok(new { message = "登陆成功", data = weIDToken, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        // GET: api/values/5
        /// <summary>
        /// 获取学籍信息
        /// </summary>
        /// <param name="studentNumber"></param>
        /// <returns></returns>
        [HttpGet("getEnrollment")]
        [Authorize(Roles = "Student, School")]
        public ActionResult<EnrollmentClaim> GetEnrollment([FromQuery]string schoolName,[FromQuery]string studentNumber)
        {
            try
            {
                EnrollmentClaim e = enrollmentService.GetEnrollment(schoolName, studentNumber);
                return Ok(new { message = "获取学籍成功", data = e, code = 200 });
            } catch(Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        // GET: api/values/5
        /// <summary>
        /// 获取成绩信息
        /// </summary>
        /// <param name="studentNumber"></param>
        /// <returns></returns>
        [HttpGet("getGrade")]
        [Authorize(Roles = "Student, School")]
        public ActionResult<List<GradeDetail>> GetGrade([FromQuery]string schoolName, [FromQuery]string studentNumber, [FromQuery]string keyword)
        {
            try
            {
                List<GradeDetail> g = gradeService.GetGrade(schoolName, studentNumber, keyword);
                return Ok(new { message = "获取成绩成功", data = g, code = 200 });
            } catch(Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        // POST api/values
        /// <summary>
        /// 下载学籍信息
        /// </summary>
        [HttpGet("downloadEnrollment")]
        public FileContentResult DownloadEnrollment([FromQuery]string schoolName, [FromQuery]string studentNumber)
        {
            return File(enrollmentService.DownloadEnrollment(schoolName, studentNumber), "application/octet-stream", studentNumber + "学籍.pdf");
        }

        // POST api/values
        /// <summary>
        /// 下载成绩信息
        /// </summary>
        [HttpGet("downloadGrade")]
        public FileContentResult DownloadGrade([FromQuery] string schoolName, [FromQuery]string studentNumber)
        {
            return File(gradeService.DownloadGrade(schoolName, studentNumber), "application/octet-stream", studentNumber + "成绩.pdf");
        }
    }
}
